# 安装编译依赖组件。 sudo apt install -y libncurses5-dev libssl-dev bison flex libelf-dev gcc make openssl libc6-dev sudo apt install -y flex bison make gcc libssl-dev bc libelf-dev wget https://mirrors.tuna.tsinghua.edu.cn/kernel/v4.x/linux-4.18.tar.gz tar -zxf linux-4.18.tar.gz cd linux-4.18.tar.gz
# 设置调试的编译菜单 export ARCH=x86 make x86_64_defconfig make menuconfig
# 下面选项如果没有选上的,选上(点击空格键),然后 save 保存设置,退出 exit。 ################################################################## General setup ---> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
Device Drivers ---> [*] Block devices ---> <*> RAM block device support (65536) Default RAM disk size (kbytes)
Processor type and features ---> [*] Randomize the address of the kernel image (KASLR) Kernel hacking ---> Compile-time checks and compiler options ---> [*] Compile the kernel with debug info [*] Provide GDB scripts for kernel debugging
Device Drivers --> Network device support --> <*> Universal TUN/TAP device driver support
[*] Networking support --> Networking options --> <*> 802.1d Ethernet Bridging # 编译内核。 make -j8
2. Build the kernel
With the prerequisites installed and the kernel configuration set, build the kernel. Here are two errors I ran into and the changes that got me past them.
2.1 Networking header issues
1 2 3 4 5 6 7 8 9
In file included from scripts/selinux/genheaders/genheaders.c:19: # scripts/selinux/mdp/mdp.c 也会出现这种现象 ./security/selinux/include/classmap.h:249:2: error: #error New address family defined, please update secclass_map. 249 | #error New address family defined, please update secclass_map. | ^~~~~ make[3]: *** [scripts/Makefile.host:90: scripts/selinux/genheaders/genheaders] Error 1 make[2]: *** [scripts/Makefile.build:558: scripts/selinux/genheaders] Error 2 make[1]: *** [scripts/Makefile.build:558: scripts/selinux] Error 2 make: *** [Makefile:1045: scripts] Error 2
This is the fix that worked for me:
Open the two C files named in the error, genheader.c and mdp.c, and remove this include from both:
1
#include<sys/socket.h>
Find the classmap.h file named in the error: a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h Add the following include to classmap.h:
1
#include<linux/socket.h>
2.2 Linker issues
1 2 3 4
arch/x86/entry/entry_64.S: Assembler messages: arch/x86/entry/entry_64.S:1667: Warning: no instruction mnemonic suffix given and no register operands; using default for `sysret' AS arch/x86/entry/thunk_64.o arch/x86/entry/thunk_64.o: warning: objtool: missing symbol table
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "kernel-debug", "type": "cppdbg", "request": "launch", "miDebuggerServerAddress": "192.168.1.114:1234", "program": "${workspaceFolder}/vmlinux", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "logging": { "engineLogging": false }, "MIMode": "gdb", } ] }
Start QEMU, attach the VS Code debugger, and you can step through the kernel.
There was still one snag: as the screenshot shows, GDB reports many variables as “optimized out.” The kernel was built with O2 optimization by default, but simply switching to O0 broke the build. Fixing that would have required further changes, so I left it as-is and used single-stepping and memory inspection instead.